package com.scrx.ymg.service.user;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.scrx.ymg.dao.agent.IAgentInfoDao;
import com.scrx.ymg.dao.distribution.IDistributionDao;
import com.scrx.ymg.dao.supermarket.ISuperMarketDao;
import com.scrx.ymg.dao.user.IUserDao;
import com.scrx.ymg.entity.AgentDrawings;
import com.scrx.ymg.entity.AgentInfo;
import com.scrx.ymg.entity.Distribution;
import com.scrx.ymg.entity.SuperMarketInfo;
import com.scrx.ymg.entity.User;
import com.scrx.ymg.util.BaseUtil;
import com.scrx.ymg.util.CodeUtil;
import com.scrx.ymg.util.PayConfigUtil;




@Service("userService")
public class UserServiceImpl implements IUserService {
	
	@Autowired
	private IUserDao userDao;
	
	@Autowired
	private ISuperMarketDao superdao;
	
	@Autowired
	private IAgentInfoDao agentdao;
	
	@Autowired
	private IDistributionDao disdao;
	
	
	public List<User> hello(){
		return userDao.hello();
	}
	
	public List<User> findAll(){
		return userDao.findAll();
	}


	public User findById(Integer id) {
		return userDao.findById(id);
	}
	
	public void save(User user){
		    userDao.save(user);
	}


	public boolean saveifnull(User user) {
		boolean temp=false;
		User usr=userDao.findByUserID(user.getUserId());
		if(usr==null){
			 userDao.save(user);
			 temp=true;
		}

		return temp;
	}
	public String saveifnull(String userName,String openID,int referrerId,String headportrait) {
		SimpleDateFormat  formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
		String temp="";
		User userentity=userDao.findByUserID(openID);
		if(userentity==null){
			User user =new User();
			user.setUserId(openID);
			user.setUserName(userName);
			user.setOrderNum(0);
			user.setConsumeMoney(0.0);
			user.setReferrerId(referrerId);
			user.setHeadportrait(headportrait);
			user.setRegistrationDate(formatter.format(new Date()));
			userentity=userDao.saveGetUserID(user);
			Distribution db=new Distribution();
			db.setUserid(userentity.getId());
			db.setFxBrokerage(0.0);
			db.setTxBrokerage(0.0);
			db.setKtxBrokerage(0.0);
			disdao.insertDistribution(db);
			String qrCode =CodeUtil.getminiqrQr(userentity.getId().toString(), PayConfigUtil.access_token);
			userentity.setQrCode(qrCode);
			userDao.updateObject(userentity);
		}
		return userentity.getId().toString();
	}


	public List<User> getMemberInfoBySuperID(int superId) {
		return userDao.getMemberInfoBySuperID(superId);
	}


	public Map<Object, Object> getUserInfo(User user, int pageNum, int pageCount) {

		return userDao.getUserInfo(user,pageNum,pageCount);
	}



	public Map<Object, Object> getUserInfo(User user, String agentRegion, String superName, int pageNum,
			int pageCount) {
		
		SuperMarketInfo superinfo = superdao.getsuperMarketInfoBySuperName(superName);
		AgentInfo agentinfo = agentdao.getAgentInfoByagentRegion(agentRegion);
		if(superinfo!=null){
			user.setBelongSupermarket(superinfo.getId());
		}else{
			if(superName!=""){
				user.setBelongSupermarket(-1);
			}
		}
		if(agentinfo!=null){
			user.setAgentId(agentinfo.getId());
		}else{
			if(agentRegion!=""){
				user.setAgentId(-1);
			}
			
		}
		return userDao.getUserInfo(user,pageNum,pageCount);
	}


	public String updateUser(User user) {

		return userDao.updateUser(user);
	}


	public List<User> getUsers(User user, int pageNum, int pageCount) {
		return userDao.getUsers(user, pageNum, pageCount);
	}

	public String userDrawings(AgentDrawings drawing) {
		Distribution distribution=disdao.getDistribution(drawing.getMerchantId());
		drawing.setWithdrawMoney(distribution.getKtxBrokerage());
		drawing.setStatus(0);
		drawing.setSubmitTime(BaseUtil.newdate());
		distribution.setKtxBrokerage(0.0);
		if(drawing.getWithdrawMoney()>=100){
			return userDao.insertAndUpdateObjects(drawing,distribution);
		}
		return "false";
	}


	public String updateObjects(List<Object> objects) {
		return userDao.updateObjects(objects);
	}


	public Map<Object, Object> getUserXiaXian(int userId) {

		return userDao.getUserXiaXian(userId);
	}

}
